package com.ce.pms.charge.dao.impl;


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.IBankAccountDao;
import com.ce.pms.charge.model.BankAccountDomain;
import com.ce.util.BlankUtil;
public class BankAccountDaoImpl extends PmsBaseDaoImpl<BankAccountDomain> implements IBankAccountDao{

	/***
	 * 功能描述: 根据房间ID查找银行帐号 <br>
	 * @param houseId
	 * @param validity 是否加可用限制
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-7-31  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BankAccountDomain getBankAccountByHouseId(Long houseId, boolean validity) {
		BankAccountDomain bankAccount = null;
		StringBuffer hql = new StringBuffer( "from BankAccountDomain bankAccount where bankAccount.house.id = ? ");
		if(validity) {
			hql.append(" and bankAccount.isActive=1");
		}
		List<BankAccountDomain> list = createQuery(hql.toString(), houseId).list();
		if(!BlankUtil.isBlank(list))
			bankAccount = list.get(0);
		return bankAccount;
	}

	/**
	 * 功能描述: 根据产权ID查找银行帐号 <br>
	 * @param proRightId 产权ID
	 * @param validity 是否只查询有效的实体 true：是；false：否
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BankAccountDomain getBankAccountByProRightId(Long proRightId, boolean validity) {
		BankAccountDomain bankAccount = null;
		StringBuffer hql = new StringBuffer( "from BankAccountDomain bankAccount where bankAccount.proRight.id=? ");
		if(validity) {
			hql.append("and bankAccount.isActive=1");
		}
		List<BankAccountDomain> list = createQuery(hql.toString(), proRightId).list();
		if(!BlankUtil.isBlank(list))
			bankAccount = list.get(0);
		return bankAccount;
	}

	/**
	 * 功能描述: 根据房间ID串查找银行帐号 <br>
	 * @param houseIds
	 * @param validity 是否加可用限制
	 * @return
	 * Create author:yandawei <br>
	 * Create on:2008-7-31  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<BankAccountDomain> getBankAccountListByHouseIds(String houseIds , boolean validity) {
		List<BankAccountDomain> bankAccounts = new ArrayList<BankAccountDomain>();
		StringBuffer hql = new StringBuffer("from BankAccountDomain bankAccount where bankAccount.house.keyword in ("+houseIds+") ");
		if(validity)
		{
			hql.append(" and bankAccount.isActive=1");
		}
		bankAccounts = (List<BankAccountDomain>) super.createQuery(hql.toString(), null).list();
		return bankAccounts;
	}

	/**
	 * 功能描述: 根据银行id查询银行账号数量 <br>
	 * @param bankId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Long getBankAccountCountByBankId(Long bankId){
		String sql = "SELECT COUNT(FBA.BANK_ACCOUNT_ID) FROM FEE_BANK_ACCOUNT FBA WHERE FBA.BANK_ID=? ";
		Object obj = super.createSQLQuery(sql, bankId).uniqueResult();
		return ((BigInteger)obj).longValue();
	}
}
